﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.Snowflake;
using ZilLion.Service.ZilLionOss.Domain.Entitys;
using ZilLion.Service.ZilLionOss.Domain.Repositorys.Interface;
using ZilLion.Service.ZilLionOss.Domain.Services.Interface;

namespace ZilLion.Service.ZilLionOss.Domain.Services
{
    public class OssUserService : IOssUserService
    {
        private readonly IOssRoleRepository _ossRoleRepository;
        private readonly IOssRoleRouteRepository _ossRoleRouteRepository;
        private readonly IOssUserInfoRepository _ossUserInfoRepository;
        private readonly IOssUserPwdRepository _ossUserPwdRepository;
        private readonly IOssUserRoleRepository _ossUserRoleRepository;

        public OssUserService(IOssUserPwdRepository ossUserPwdRepository, IOssUserInfoRepository ossUserInfoRepository,
            IOssUserRoleRepository ossUserRoleRepository, IOssRoleRepository ossRoleRepository,
            IOssRoleRouteRepository ossRoleRouteRepository)
        {
            _ossUserPwdRepository = ossUserPwdRepository;
            _ossUserInfoRepository = ossUserInfoRepository;
            _ossUserRoleRepository = ossUserRoleRepository;
            _ossRoleRepository = ossRoleRepository;
            _ossRoleRouteRepository = ossRoleRouteRepository;
        }

        public OssUserInfoEntity InitUser(string username, string zlcompanykey)
        {
            var userid = SnowflakeIdCreater.Instance().GetNextId();
            using (var unit = new UnitOfWork())
            {
                //  --初始密码123456(加盐为zillion)
                _ossUserPwdRepository.SavePwd(
                    new OssUserPwdEntity
                    {
                        Userid = userid,
                        Username = username,
                        Userpwd = "1047219cb50e359761667f4e2ce76b9a"
                    }, unit);
                _ossUserInfoRepository.SaveUserInfo(
                    new OssUserInfoEntity
                    {
                        UserId = userid,
                        UserName = username,
                        UserNickName = username,
                        UserAvatar = string.Empty,
                        UserPhoneNo = 0,
                        ZlCompanyKey = zlcompanykey
                    }, unit);
                unit.Complete();
            }
            var savedinfo = _ossUserInfoRepository.GetUserInfo(userid);
            return savedinfo;
        }

        public void SaveUserInfo(OssUserInfoEntity entity)
        {
            _ossUserInfoRepository.SaveUserInfo(entity);
        }

        public void SaveUserpwd(OssUserPwdEntity entity)
        {
            _ossUserPwdRepository.SavePwd(entity);
        }

        public OssUserPwdEntity GetOssUserPwdByUserId(long userid)
        {
            return _ossUserPwdRepository.GetPwdEntity(userid);
        }


        public OssRoleEntity SaveRole(OssRoleEntity entity)
        {
            return _ossRoleRepository.SaveOssRole(entity);
        }

        public void SetRoleRoutes(long roleid, IList<string> routepaths)
        {
            using (var unit = new UnitOfWork())
            {
                _ossRoleRouteRepository.DeleteRouteByRoleid(roleid, unit);
                foreach (var routepath in routepaths)
                {
                    var ossUserRoleEntity = new OssRoleRouteEntity
                    {
                        Roleid = roleid,
                        RoutePath = routepath
                    };
                    _ossRoleRouteRepository.AddOssRoleRoute(ossUserRoleEntity, unit);
                }
                unit.Complete();
            }
        }

        public void SerUserRoles(long userid, IList<long> roles)
        {
            using (var unit = new UnitOfWork())
            {
                _ossUserRoleRepository.DeleteRoleByUserid(userid, unit);
                //OssRoleRouteEntity
                foreach (var roleid in roles)
                {
                    var ossUserRoleEntity = new OssUserRoleEntity
                    {
                        RoleId = roleid,
                        UserId = userid
                    };
                    _ossUserRoleRepository.AddOssUserRoles(ossUserRoleEntity, unit);
                }
                unit.Complete();
            }
        }

        public IList<OssUserInfoEntity> GetAllUsers()
        {
            return _ossUserInfoRepository.GetAllUsers().ToList();
        }

        public OssUserInfoEntity GetUserInfo(long userid)
        {
            return _ossUserInfoRepository.GetUserInfo(userid);
        }

        public OssUserInfoEntity GetUserInfoByUsername(string username)
        {
            return _ossUserInfoRepository.GetUserInfo(username);
        }

        public IEnumerable<OssRoleRouteEntity> GetOssRoleRoutesByRoleid(long roleid)
        {
            return _ossRoleRouteRepository.GetOssRoleRoutesByRoleid(roleid);
        }

        public IEnumerable<OssUserRouteEntity> GetOssRoleRoutesByUserid(long userid)
        {
            return _ossRoleRouteRepository.GetOssRoleRoutesByUserid(userid);
        }


        public IEnumerable<OssRoleEntity> GetUserRoles(long userid)
        {
            return _ossRoleRepository.GetOssUserRoles(userid);
        }

        public void DeleteUser(long userid)
        {
            using (var unit = new UnitOfWork())
            {
                _ossUserInfoRepository.DeleteUser(userid, unit);
                _ossUserPwdRepository.DeleteUser(userid, unit);
                _ossUserRoleRepository.DeleteRoleByUserid(userid, unit);
                unit.Complete();
            }
        }
    }
}